package com.fox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fox.config.constant.SystemConstants;
import com.fox.domain.entity.Menu;
import com.fox.mapper.MenuMapper;
import com.fox.service.MenuService;
import com.fox.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2024-01-29 15:55:52
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<String> selectPermsByUserId(Long id) {
        //如果是管理员，返回所有的权限
        if (id == 1L) {
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Menu::getMenuType, SystemConstants.MENU_SYSTEM,SystemConstants.BUTTON_SYSTEM);
            wrapper.eq(Menu::getStatus,SystemConstants.STATUS_NORMAL);
            List<Menu> menus = list(wrapper);
            List<String> perms = new ArrayList<>();
            for (Menu menu : menus) {
                perms.add(menu.getPerms());
            }
            return perms;
        }
        //否则返回其所对应的权限
        return getBaseMapper().selectPermsByUserId(id);
    }

    @Override
    public List<Menu> selectRouterMenuTreeByUserId(Long userId) {
        //判断是否是管理员
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        if (SecurityUtils.isAdmin()) {
            //如果是 获取所有符合要求的menu
            menus = menuMapper.selectAllRouterMenu();
        }else {
            //否则 获取用户所具有的Menu
            menus = menuMapper.selectRouterMenuTreeByUserId(userId);
        }
        //构建tree
        //先找出第一层的菜单，然后去找他们的子菜单设置到children属性中
        List<Menu> menuTree = builderMenuTree(menus,0L);
        return menuTree;
    }

    @Override
    public List<Menu> selectMenuList(Menu menu) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        //根据MenuName进行模糊查询
        queryWrapper.like(StringUtils.hasText(menu.getMenuName()),Menu::getMenuName,menu.getMenuName());
        queryWrapper.eq(StringUtils.hasText(menu.getMenuType()),Menu::getMenuType,menu.getMenuType());
        //排序 parent_id 和 order_num
        queryWrapper.orderByAsc(Menu::getParentId,Menu::getOrderNum);
        List<Menu> menus = getBaseMapper().selectList(queryWrapper);
        return menus;
    }

    @Override
    public boolean hasChild(Long menuId) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId,menuId);
        return count(queryWrapper) != 0;
    }

    @Override
    public List<Long> selectMenuIdListByRoleId(Long roleId) {
        return getBaseMapper().selectMenuIdListByRoleId(roleId);
    }

    private List<Menu> builderMenuTree(List<Menu> menus, Long parentId) {
        List<Menu> menuTree = new ArrayList<>();

        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                menu.setChildren(getChildren(menu, menus));
                menuTree.add(menu);
            }
        }
        return menuTree;
    }

    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = new ArrayList<>();
        for (Menu m : menus) {
            if (m.getParentId().equals(menu.getId())) {
                m.setChildren(getChildren(m, menus));
                childrenList.add(m);
            }
        }
        return childrenList;
    }


}

